package com.zqxx.examine.management.controller.page.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.management.service.QuestionHistoryService;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.management.service.TrainingService;
import com.zqxx.examine.model.practice.KnowledgePointAnalysisResult;
import com.zqxx.examine.model.practice.TypeAnalysis;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.question.QuestionStatistic;
import com.zqxx.examine.model.training.Training;
import com.zqxx.examine.model.training.TrainingSection;
import com.zqxx.examine.model.training.UserTraining;

@Controller
public class TrainingPageAdmin
{
  @Autowired
  private TrainingService trainingService;
  @Autowired
  private QuestionService questionService;
  @Autowired
  private QuestionHistoryService questionHistoryService;
  
  @RequestMapping(value={"/admin/training/training-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String trainingListPage(Model model, HttpServletRequest request, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    Page<Training> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(10);
    List<Training> trainingList = this.trainingService.getTrainingList(-1, pageModel);
    String pageStr = PagingUtil.getPageBtnlink(page, pageModel
      .getTotalPage());
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("trainingList", trainingList);
    return "training-list";
  }
  
  @RequestMapping(value={"/admin/training/training-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String trainingAddPage(Model model, HttpServletRequest request)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    model.addAttribute("fieldList", fieldList);
    return "training-add";
  }
  
  @RequestMapping(value={"/admin/training/student-practice-status/{userId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String practiceStatusDefaultPage(Model model, HttpServletRequest request, @PathVariable int userId)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    return "redirect:/admin/training/student-practice-status/" + ((Field)fieldList.get(0)).getFieldId() + "/" + userId;
  }
  
  @RequestMapping(value={"/admin/training/student-practice-status/{fieldId}/{userId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String practiceStatusPage(Model model, HttpServletRequest request, @PathVariable int fieldId, @PathVariable int userId)
  {
    Map<Integer, Map<Integer, QuestionStatistic>> questionMap = this.questionService.getTypeQuestionStaticByFieldId(fieldId);
    Map<Integer, Map<Integer, QuestionStatistic>> historyMap = this.questionHistoryService.getTypeQuestionHistStaticByFieldId(fieldId, userId);
    
    List<KnowledgePointAnalysisResult> kparl = new ArrayList();
    for (Map.Entry<Integer, Map<Integer, QuestionStatistic>> entry : questionMap.entrySet())
    {
      KnowledgePointAnalysisResult kpar = new KnowledgePointAnalysisResult();
      kpar.setKnowledgePointId(((Integer)entry.getKey()).intValue());
      List<TypeAnalysis> tal = new ArrayList();
      int totalRightAmount = 0;
      int totalAmount = 0;
      Map<Integer, QuestionStatistic> a=(Map)entry.getValue();
      for (Map.Entry<Integer, QuestionStatistic> entry1 : a.entrySet())
      {
        TypeAnalysis ta = new TypeAnalysis();
        ta.setQuestionTypeId(((Integer)entry1.getKey()).intValue());
        ta.setQuestionTypeName(((QuestionStatistic)entry1.getValue()).getQuestionTypeName());
        int rightAmount = 0;
        int wrongAmount = 0;
        try
        {
          rightAmount = ((QuestionStatistic)((Map)historyMap.get(entry.getKey())).get(entry1.getKey())).getRightAmount();
        }
        catch (Exception localException) {}
        try
        {
          wrongAmount = ((QuestionStatistic)((Map)historyMap.get(entry.getKey())).get(entry1.getKey())).getWrongAmount();
        }
        catch (Exception localException1) {}
        ta.setRightAmount(rightAmount);
        ta.setWrongAmount(wrongAmount);
        ta.setRestAmount(((QuestionStatistic)entry1.getValue()).getAmount() - rightAmount - wrongAmount);
        tal.add(ta);
        if (kpar.getKnowledgePointName() == null) {
          kpar.setKnowledgePointName(((QuestionStatistic)entry1.getValue()).getPointName());
        }
        totalRightAmount += rightAmount;
        totalAmount += ((QuestionStatistic)entry1.getValue()).getAmount();
      }
      kpar.setTypeAnalysis(tal);
      if (totalAmount > 0) {
        kpar.setFinishRate(totalRightAmount / totalAmount);
      }
      kparl.add(kpar);
    }
    model.addAttribute("kparl", kparl);
    model.addAttribute("fieldList", this.questionService.getAllField(null));
    model.addAttribute("fieldId", Integer.valueOf(fieldId));
    return "practice-status";
  }
  
  @RequestMapping(value={"/admin/training/student-training-history/{trainingId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String trainingHistoryPage(Model model, HttpServletRequest request, @PathVariable int trainingId, @RequestParam(value="searchStr", required=false, defaultValue="") String searchStr, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    Page<UserTraining> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(10);
    List<UserTraining> userTrainingList = this.trainingService.getUserTrainingList(trainingId, -1, searchStr, pageModel);
    List<Integer> idList = new ArrayList();
    for (UserTraining userTraining : userTrainingList) {
      idList.add(Integer.valueOf(userTraining.getUserId()));
    }
    Object map = this.trainingService.getTrainingSectionProcessMapByTrainingId(trainingId, idList, searchStr);
    String pageStr = PagingUtil.getPageBtnlink(page, pageModel
      .getTotalPage());
    model.addAttribute("trainingId", Integer.valueOf(trainingId));
    model.addAttribute("userTrainingList", userTrainingList);
    model.addAttribute("processMap", map);
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("searchStr", searchStr);
    return "training-history";
  }
  
  @RequestMapping(value={"/admin/training/section-list/{trainingId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String sectionPage(Model model, HttpServletRequest request, @PathVariable int trainingId)
  {
    List<TrainingSection> sectionList = this.trainingService.getTrainingSectionByTrainingId(trainingId, -1, null);
    model.addAttribute("sectionList", sectionList);
    return "section";
  }
}
